/**
 * @(#) MedicalMoneyProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	건강보험증 Proxy - Proxy
 * 				건강보험증를 조회를한다.
 */
package com.skt.common_hr.holcommnos.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.holcommnos.model.vo.codeVO;
	import com.skt.common_hr.holcommnos.model.vo.mainListVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class HolcommnosProxy extends CommonProxy
	{
		public static const NAME:String = "HolcommnosProxy";

		public var mainList:ArrayCollection;
		public var acTeamMembers:ArrayCollection;

		public static var ins:HolcommnosProxy;
		public static function getInstance():HolcommnosProxy
		{
			if(ins==null)ins = new HolcommnosProxy;
			return ins;	
		}
		
		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function HolcommnosProxy(data:Object=null)
		{
			super(NAME, data);
		}


		
		
		// 메인리스트 호출
		public function loadList(dfStart:Date, dfEnd:Date, setFunc:Function, I_BPERNR:String = ''):void
		{
			var obj:Object = {};

			obj.I_PERNR 	= Variables.myPernr;
			obj.I_BPERNR  = I_BPERNR;
			obj.I_BEGDA	= dfStart;
			obj.I_ENDDA	= dfEnd;		
			obj = toJsonType(obj);
			loadData("Z_HR_PW01", obj, loadListResult, listFault, setFunc);
		}
		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				mainList 			= parseMainList(event.result.OTAB);
				acTeamMembers 	= event.result.OTAB1;
				acTeamMembers.addItemAt({'ENAME':'전체'}, 0);
				
				var O_TEAM:String = event.result.O_TEAM;
				event.result.func(mainList, acTeamMembers, O_TEAM);//loadMainList
			}
			else {
				//Alert.show(retText);
				mainList = new ArrayCollection();
				event.result.func(mainList, acTeamMembers, O_TEAM);
			}
		}

		// 저장
		public function saveOrderList(dgDataList:ArrayCollection,  setFunc:Function, dv:String='INS'):void
		{
			var tables:Object = new Object();
			var params:Object = new Object();			
 			var table:Array = [];
 			var oSelectedItem:Object;
			for(var i:int=0; i<dgDataList.length; i++) {
				oSelectedItem = {};
				oSelectedItem = dgDataList.getItemAt(i);
				table.push(oSelectedItem);
			}

			params["I_PERNR"] 	= Variables.myPernr;//oSelectedItem.PERNR;		//사원번호
			params["I_ACTIO"] 	= dv;						//상태
			tables["ITAB"] = table;
			saveData("Z_HR_PW02", params, tables, saveOrderListResult, listFault, setFunc);					
		}

		
		public function saveOrderListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				Alert.show(retText);
			}
			else {
				Alert.show(retText);
				return;
			}
			event.result.func();
		}

      private function parseMainList(recArr:ArrayCollection):ArrayCollection
      {
           var retArr:ArrayCollection = new ArrayCollection();

           for each(var obj:Object in recArr){
                var retObj:mainListVO = new mainListVO(); 

                for(var field:String in obj) {
                     if(retObj.hasOwnProperty(field)==false ) {
                          continue;
                     }
					
					if(field == "BEGDA" || field == "ENDDA" || field == "PMTDT" ) {
                          retObj[field] = DateUtil.stringToDateTime(obj[field]);
                          retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);
                          retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);
                     }                                                      
                     else {                             
                          retObj[field] = obj[field];
                     }
                }
                retArr.addItem(retObj);
           }
           return retArr;
      }


      private function parseCodeList(recArr:ArrayCollection):ArrayCollection
      {
           var retArr:ArrayCollection = new ArrayCollection();

           for each(var obj:Object in recArr){
                var retObj:codeVO = new codeVO();

                for(var field:String in obj) {
                     if(retObj.hasOwnProperty(field)==false ) {
                          continue;
                     }
					
					if(field is Date) {                                        
                          retObj[field] = DateUtil.stringToDateTime(obj[field]);
                          retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);
                          retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);
                     }                                                      
                     else {                             
                          retObj[field] = obj[field];
                     }
                }
                retArr.addItem(retObj);
           }
           return retArr;
      }
		
		private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);
				}
				else {
					retObj[field] = vo[field];
				}
				trace(retObj[field]);
			} 
			return retObj;
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}
	}
}